package com.shenjies88.practice.baseframework.controller;

import com.shenjies88.practice.baseframework.entity.ActorFilms;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * @author by shenjies88
 * @since 2025/9/13 19:26
 */
@Slf4j
@AllArgsConstructor
@RequestMapping("/chatClient")
@RestController
public class ChatClientController {

    @Resource(name = "qwen3ChatClient")
    private final ChatClient qwen3ChatClient;

    @Resource(name = "dsr1ChatClient")
    private final ChatClient dsr1ChatClient;

    @Resource(name = "dsllmChatClient")
    private final ChatClient dsllmChatClient;

    @GetMapping("/chat")
    public String chat(@RequestParam String msg) {
        ChatClient.CallResponseSpec call = dsr1ChatClient.prompt()
                .user(msg)
                .call();
        return call.content();
    }

    @GetMapping("/chatEntity")
    public ActorFilms chatEntity(@RequestParam String msg) {
        ChatClient.CallResponseSpec call = dsllmChatClient.prompt()
                .user(msg)
                .call();
        return call.entity(ActorFilms.class);
    }

    @GetMapping("/chatEntityList")
    public List<ActorFilms> chatEntityList(@RequestParam String msg) {
        ChatClient.CallResponseSpec call = dsllmChatClient.prompt()
                .user(msg)
                .call();
        return call.entity(new ParameterizedTypeReference<>() {
        });
    }

    @GetMapping("/streamChat")
    public Flux<String> streamChat(@RequestParam String msg) {
        Flux<String> content = dsllmChatClient.prompt()
                .user(msg)
                .stream()
                .content();
        return content.mapNotNull(resp -> {
            log.info(resp);
            return resp;
        });
    }
}
